package com.abm;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


public class RestActivity extends Activity {

	LinearLayout lLayout;
	
	TextView txt_id;
	TextView txt_level;
	TextView txt_timestamp;
	
	EditText edt_id;
	EditText edt_level;
	EditText edt_timestamp;
	
	Button btn_cargar;
	
	String id;
	String level;
	String timestamp;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ScrollView sv = new ScrollView(this);
		sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));

		lLayout = new LinearLayout(this);
		lLayout.setOrientation(LinearLayout.VERTICAL);
		lLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));

		sv.addView(lLayout);

		txt_id = new TextView(this);
		txt_level = new TextView(this);
		txt_timestamp = new TextView(this);
		
		txt_id.setText(R.string.id);
		txt_level.setText(R.string.level);
		txt_timestamp.setText(R.string.timestamp);
		
		txt_id.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		txt_level.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		txt_timestamp.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		
		edt_id = new EditText(this);
		edt_level = new EditText(this);
		edt_timestamp = new EditText(this);
		
		btn_cargar = new Button(this);
		btn_cargar.setText(R.string.load);
		btn_cargar.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				new LongRunningGetIO().execute();
			}
		});
		
		
		lLayout.addView(txt_id);
		lLayout.addView(edt_id);
		lLayout.addView(txt_level);
		lLayout.addView(edt_level);
		lLayout.addView(txt_timestamp);
		lLayout.addView(edt_timestamp);
		lLayout.addView(btn_cargar);

		setContentView(sv);
		
		
	}


	private class LongRunningGetIO extends AsyncTask<Void, Void, String> {		


		@Override
		protected String doInBackground(Void... params) {
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet get =
				    new HttpGet("http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=1");
		    get.setHeader("content-type", "application/json");
			
		    try
		    {
		            HttpResponse resp = httpClient.execute(get);
		            String respStr = EntityUtils.toString(resp.getEntity());
		     
		            JSONArray respJSON = new JSONArray(respStr);
		            JSONObject elemento = respJSON.getJSONObject(0);
		            
		            id = elemento.getString("id");
		            level = elemento.getString("level");
		            timestamp = elemento.getString("time_stamp");
		            
		            return "Ok";

		    }
		    catch(Exception ex)
		    {
		            Log.e("ServicioRest","Error!", ex);
		    }
			return null;
		}

		
		protected void onPostExecute(String results) {
			if (results.equalsIgnoreCase("Ok")) {
				edt_id.setText(id);
	            edt_level.setText(level);
	            edt_timestamp.setText(timestamp);
			}
		}
	}


}
